package com.atguigu.chapter06;

import com.atguigu.Bean.UserBehavior;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import org.junit.Test;

/**
 * @ClassName: Flink01_Web_PV
 * @Description:
 * @Author: kele
 * @Date: 2021/4/6 18:31
 *
 * project1：
 *   统计网页浏览量
 *
 *   数据来源：UserBehavior.csv
 *
 *   1、读取数据
 *   2、将数据封装为UserBehavior类型
 *      过滤得到pv的数据
 *
 * 方式二
 *      1、读取数据
 *      2、封装为UserBehavior
 *      3、按照行为进行keyby
 *      4、process进行处理
 *          **因为keyby之后数据都进入了同一个分区，所以可以使用sum
 *
 *
 **/
public class Flink01_Web_PV {

    @Test
    public void Method1() {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port",20000);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        DataStreamSource<String> ds = env.readTextFile("in/UserBehavior.csv");

        ds.flatMap(new FlatMapFunction<String, Tuple2<String,Long>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Long>> out) throws Exception {

                String[] info = value.split(",");

                UserBehavior userBehavior = new UserBehavior(Long.valueOf(info[0]),
                        Long.valueOf(info[1]),
                        Integer.valueOf(info[2]),
                        info[3],
                        Long.valueOf(info[4])
                );

                if("pv".equals(userBehavior.getBehavior())){
                    out.collect(Tuple2.of(userBehavior.getBehavior(),1l));
                }
            }
        }).keyBy(d->d.f0)
                .sum(1)
                .print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    @Test
    public void Method2(){

        Configuration conf = new Configuration();
        conf.setInteger("rest.port",20000);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        DataStreamSource<String> ds = env.readTextFile("in/UserBehavior.csv");

        ds.map(line->{
            String[] info = line.split(",");
            
            return  new UserBehavior(Long.valueOf(info[0]),
                    Long.valueOf(info[1]),
                    Integer.valueOf(info[2]),
                    info[3],
                    Long.valueOf(info[4]));
        })
                .keyBy(UserBehavior::getBehavior)
                .process(new KeyedProcessFunction<String, UserBehavior, Tuple2<String,Long>>() {

                    long sum = 0;  //sum的个数跟并行度相同，但是由于keyby，所有的pv进入同一个通道，所以可以定义这个状态

                    @Override
                    public void processElement(UserBehavior value, Context ctx, Collector<Tuple2<String, Long>> out) throws Exception {
                        if("pv".equals(value.getBehavior())){
                            sum++;
                            out.collect(Tuple2.of(value.getBehavior(),sum));
                        }
                    }
                }).print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }


}
